package com.aizhixin.test.question.dao;

import java.sql.PreparedStatement;
import java.util.List;

import org.hibernate.query.criteria.internal.expression.ConcatExpression;
import org.hibernate.sql.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.integration.IntegrationProperties.Jdbc;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import com.aizhixin.test.question.dto.addChangeDeleteQuestionDTO;
import com.aizhixin.test.question.dto.addorChangeOrDeleteoptionDTO;

@Component
public class addOrDeleteOrChangeQuestionDAO {
	@Autowired
	private JdbcTemplate jdbcTemplate;
	/**
	 * 调用这个可以增加选项
	 * @param dto
	 * @return
	 */
	public addChangeDeleteQuestionDTO addquestion(addChangeDeleteQuestionDTO dto) {
		final String sql="INSERT INTO t_question (ID,CONTENT,KNOWLEDGE_AREA,ANSWER,CREATED_DATE) VALUES(?,?,?,?,?,NOW())";
		try {
			jdbcTemplate.update(ConcatExpression ->{
				PreparedStatement ps =ConcatExpression.prepareStatement(sql);
				ps.setLong(1, dto.getId());
				ps.setString(2, dto.getContent());
				ps.setString(3, dto.getKnowledgeArea());
				ps.setString(4, dto.getAnswer());
				return ps;
			});
			return dto;
		} catch (Exception e) {
			return null;
		}
		
	}
	/**
	 * 调用这个可以增加选项（分开的目的：可以整多选）
	 * @param dto
	 * @return
	 */
	public addorChangeOrDeleteoptionDTO addoption(addorChangeOrDeleteoptionDTO dto) {
		final String sql="insert into t_question_option (OPTION_CONTENT,`OPTION`,QUESTION_ID,CREATED_DATE)VALUES(?,?,?,NOW())";
		try {
			jdbcTemplate.update(ConcatExpression ->{
				PreparedStatement ps=ConcatExpression.prepareStatement(sql);	
				ps.setString(1, dto.getOptionContent());
				ps.setString(2, dto.getOption());
				ps.setLong(3, dto.getQuestionID());
				return ps;});
			return dto;
		} catch (Exception  e) {
			return null;
		}
		
	}
	/**
	 * 找到最后一个试题的ID
	 * @return
	 */
	public Long seleceLastSteamId() {
		String sql="SELECT q.ID  FROM t_question as q ORDER BY q.ID  DESC LIMIT 0,1";
		Long  steamid=1+jdbcTemplate.queryForObject(sql, Long.class);
		return steamid;
	}
	/**
	 * 调用这个可以修改题目
	 * @param dto
	 * @return
	 */
	public addChangeDeleteQuestionDTO changequestion(addChangeDeleteQuestionDTO dto) {
		final String sql=" UPDATE t_question set CONTENT=?,KNOWLEDGE_AREA=?,ANSWER=?,CREATED_DATE=NOW()WHERE ID=?";
		jdbcTemplate.update(ConcatExpression ->{
			PreparedStatement pStatement=ConcatExpression.prepareStatement(sql);
		pStatement.setString(1, dto.getContent());
		pStatement.setString(2, dto.getKnowledgeArea());
		pStatement.setString(3, dto.getAnswer());
		pStatement.setLong(4, dto.getId());
			return pStatement;
		});
	
		return dto;
	}
	/**
	 * 调用这个可以修改试题选项
	 * @param dto
	 * @return
	 */
	public addorChangeOrDeleteoptionDTO changeoption(addorChangeOrDeleteoptionDTO dto) {
		final String sql="UPDATE t_question_option set OPTION_CONTENT=?,CREATED_DATE=NOW() WHERE QUESTION_ID=? AND `OPTION`=?";
		jdbcTemplate.update(ConcatExpression ->{
			PreparedStatement pStatement=ConcatExpression.prepareStatement(sql);
		pStatement.setString(1, dto.getOptionContent());
		pStatement.setLong(2, dto.getQuestionID());
		pStatement.setString(3, dto.getOption());
			return pStatement;
		});
	
		return dto;
	}
	/**
	 * 调用这个可以根据id删除题目和题号是这个的选项等
	 * @param questionID
	 * @return
	 */
	public boolean deleteQuetionAndOption(Long questionID) {
		final String sql="DELETE  FROM t_question WHERE ID=?";
		final String sql1="DELETE FROM t_question_option WHERE QUESTION_ID=?";
		try {
			jdbcTemplate.update(ConcatExpression ->{
				PreparedStatement pStatement=ConcatExpression.prepareStatement(sql);
			pStatement.setLong(1, questionID);
				return pStatement;	});
			jdbcTemplate.update(ConcatExpression ->{
				PreparedStatement ps=ConcatExpression.prepareStatement(sql1);
				ps.setLong(1, questionID);
				return ps;
			});
			return true;
		} catch (DataAccessException e) {
			return false;
		}

	}

}
